<?php

class group extends adminControl
{

	protected function indexAction()
	{
		$rs = $this->db->get_array("SELECT * FROM cms_group");
		$this->fillData('admindb', $rs);
		$this->display('group');
	}
	
	protected function getAddAction()
	{
		$this->fillData('menus', $GLOBALS['menus']);
		$this->display('group');
	}
	
	protected function postAddAction()
	{
		$groupname = Char_cv(trim($_POST['groupname']));
		if (empty($groupname)) Showmsg('请输入用户组组名');
		$ifcheck = intval($_POST['ifcheck']);
		$ifupload = intval($_POST['ifupload']);
		
		$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_group WHERE groupname='$groupname'");
		if ($rs['total'] > 0) Showmsg('相同名称的用户组已经存在');
		$this->db->update("INSERT INTO cms_group SET `ifupload`='$ifupload',`ifcheck`='$ifcheck',groupname='$groupname'");
		
		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('group');
		
		adminmsg('用户组创建成功');
		
	}
	
	protected function getEditAction()
	{
		global $ifcheck_Y, $ifcheck_N, $ifupload_Y, $ifupload_N;
		$groupid = intval($_GET['groupid']);
		$edit = $this->db->get_one("SELECT * FROM cms_group WHERE groupid='$groupid'");
		
		ifcheck($edit['ifupload'], 'ifupload');
		ifcheck($edit['ifcheck'], 'ifcheck');
		
		$this->fillData('ifupload_N', $ifupload_N);
		$this->fillData('ifupload_Y', $ifupload_Y);
		$this->fillData('ifcheck_N', $ifcheck_N);
		$this->fillData('ifcheck_Y', $ifcheck_Y);

		$edit['priv'] = explode(',',$edit['priv']);
		$this->fillData('edit', $edit);
		$this->fillData('menus', $GLOBALS['menus']);
		$this->display('group');
	}
	
	protected function postEditAction()
	{
		$groupname = Char_cv(trim($_POST['groupname']));
		$groupid = intval($_POST['groupid']);
		if (empty($groupname)) Showmsg('请输入用户组组名');
		$ifcheck = intval($_POST['ifcheck']);
		$ifupload = intval($_POST['ifupload']);
		
		$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_group WHERE groupname='$groupname' AND groupid!='$groupid'");
		if ($rs['total'] > 0) Showmsg('相同名称的用户组已经存在');
		
		$this->db->update("UPDATE cms_group SET `ifupload`='$ifupload',`ifcheck`='$ifcheck',groupname='$groupname' WHERE groupid='$groupid'");
		
		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('group');
		
		adminmsg('用户组编辑成功');		
	}
	
	protected function getDelAction()
	{
		$groupid = intval($_GET['groupid']);
		$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_user WHERE groupid='$groupid'");
		if ($rs['total'] > 0)
		{
			Showmsg('当前用户组下存在' . $rs['total'] . '名用户，无法删除此用户组');
		}
		$this->db->update("DELETE FROM cms_group WHERE groupid = '$groupid'");
		
		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('group');
				
		adminmsg('成功删除该用户组');
	}
}

new group();
?>